<?php
class VoteModel extends Db
{
    protected $_vote = 'w_vote';
    protected $_vote_user = 'w_vote_user'; //用户信息
    protected $_vote_list = 'w_vote_list';
    protected $_vote_config = 'w_vote_config'; //数据统计信息
    protected $_vote_luck = 'w_vote_luck'; //获奖用户
    protected $_ql_list = 'w_ql_list'; //抢楼详情页面
    protected $_vote_address = 'w_vote_address'; //收货地址信息
    protected $_order_kill = 'w_order_kill'; //秒杀订单
    protected $_wx_user = 'w_wx_user'; //微信用户
    

    /**
     * 
     * 增加秒杀订单数
     * @param array $params
     */
    public function addKillOrder($params)
    {
        $this->add($this->_order_kill, $params);
    }
    /**
     * 更新秒杀剩余数量
     * @param int $gid
     * @param String $key
     */
    public function saveKillNum($gid, $key, $num)
    {
        $sql = "UPDATE w_extend_int SET `int`=`int`-$num WHERE gid=$gid and `eid`='$key'";
        // $sql = "UPDATE $this->_extend_value SET $key=$key+1 WHERE gid=$gid";
        return $this->exec($sql);
    }
    
    /**
     * 获取秒杀剩余数量
     * @param int $gid
     * @param String $key
     */
    public function getKillNum($gid, $key)
    {
        $sql = "SELECT * FROM w_extend_int WHERE gid='$gid' AND `eid`='$key'";
        return $this->fetch($sql);
    }
    
    /**
     * 获取秒杀历史记录
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getKillHistoryByWhere($uid)
    {
        $sql = "SELECT a.*,b.title from w_order_kill a left join w_news_list b on a.goodsid=b.id WHERE a.uid=$uid";
        return $this->fetchAll($sql);
    }
    /**
     * 添加投票人
     * 
     * @param unknown_type $params
     */
    public function addVoteUser($params)
    {
        //  $sql = "insert into w_vote_user (username,add_time) VALUES ('$username','$add_time')";
        //  return $this->exec($sql);
        

        $this->add($this->_vote_user, $params);
    }
    
    /**
     * 添加地址
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addAddress($params)
    {
        $this->add($this->_vote_address, $params);
    }
    
    /**
     * 更新收获地址
     * Enter description here ...
     * @param unknown_type $v
     * @param unknown_type $where
     */
    public function updateAddress($v, $where)
    {
        $this->update($this->_vote_address, $v, $where);
    }
    /**
     * 是否已经存在
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getAddress($where)
    {
        return $this->getOne($this->_vote_address, $where);
    }
    
    /**
     * 抢楼顺序
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addQl($params)
    {
        $this->add($this->_ql_list, $params);
    }
    
    /**
     * 抢楼列表
     * Enter description here ...
     * @param unknown_type $start
     * @param unknown_type $limit
     */
    public function getQl($start, $limit)
    {
        $sql = "SELECT * FROM $this->_ql_list LIMIT $start,$limit";
        return $this->fetchAll($sql);
    }
    
    /**
     * 获取幸运用户
     * Enter description here ...
     */
    public function getLuck($start, $num)
    {
        $sql = "SELECT * FROM $this->_vote_luck ORDER BY id DESC LIMIT $start,$num ";
        return $this->fetchAll($sql);
    }
    
    /**
     * 更新获奖人数 全局
     * Enter description here ...
     */
    public function updateLuckNum()
    {
        $sql = "UPDATE $this->_vote_config SET `value`=`value`+1 WHERE `name`='huojiang'";
        $this->exec($sql);
    }
    /**
     * 添加幸运用户
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addLuck($params)
    {
        $this->add($this->_vote_luck, $params);
    }
    /**
     * 获取配置
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getConfig($where)
    {
        return $this->getOne($this->_vote_config, $where);
    }
    
    /**
     * 获取统计数据
     * Enter description here ...
     */
    public function getAllConfig()
    {
        return $this->getAll($this->_vote_config);
    }
    
    public function updatePeople()
    {
        $sql = "UPDATE $this->_vote_config SET `value`=`value`+1 WHERE `name`='people'";
        $this->exec($sql);
    }
    /**
     * 更新楼层
     * Enter description here ...
     */
    public function updateLc()
    {
        $sql = "UPDATE $this->_vote_config SET `value`=`value`+1 WHERE `name`='paidui'";
        $this->exec($sql);
    }
    
    /**
     * 设置配置
     * Enter description here ...
     */
    public function setConfig($v, $where)
    {
        return $this->update($this->_vote_config, $v, $where);
    }
    /**
     * 获取ip
     * Enter description here ...
     * @param unknown_type $ip
     */
    public function getVoteByCookieId($cookie)
    {
        $sql5 = "SELECT `id`,`addtime` FROM `w_vote` WHERE `cookie`='$cookie' ORDER BY addtime DESC";
        return $this->fetchAll($sql5);
    }
    
    public function saveVoteUser($v, $where)
    {
        $this->update($this->_vote_user, $v, $where);
    }
    
    /**
     * 获取投票人
     * @param unknown_type $where
     */
    public function getUserVoteNum($where)
    {
        return $this->getOne($this->_vote_user, $where);
    }
    
    public function saveExtendValue($eid, $gid)
    {
        $sql = "UPDATE w_extend_int SET `value`=`value`+1 WHERE gid=$gid and `eid`=$eid";
        // $sql = "UPDATE $this->_extend_value SET $key=$key+1 WHERE gid=$gid";
        return $this->exec($sql);
    }
    /**
     * 更新用户投票次数
     * Enter description here ...
     * @param unknown_type $username
     */
    public function saveUserVote($username)
    {
        $sql = "UPDATE $this->_vote_user SET `num`=`num`+1 WHERE `username`='$username'";
        return $this->exec($sql);
    }
    
    /**
     * 获取候选人
     * Enter description here ...
     */
    public function getVoteList()
    {
        $sql = "SELECT * FROM $this->_vote_list ORDER BY `num` DESC";
        return $this->fetchAll($sql);
    
     // return $this->getAll($this->_vote_list);
    }
    
    /**
     * 添加投票
     * Enter description here ...
     */
    public function addVote($ip, $time, $id, $name, $cookie)
    {
        $sql = "INSERT INTO `w_vote` (`id`, `ip`, `area`, `addtime`, `voteid`, `votename`,`username`) VALUES (NULL, '$ip', '', '$time', '$id', '$name','$cookie');";
        $sql2 = "UPDATE $this->_vote_list SET `num`=`num`+1 WHERE `nid`='$_POST[id]'";
        
        $this->exec($sql2);
        $this->saveUserVote($cookie);
        return $this->exec($sql);
    }
    
    /**
     * 添加投票历史记录
     * Enter description here ...
     */
    public function addVoteHistory($params)
    {
        $this->add($this->_vote, $params);
    }
    
    public function historyStatus()
    {
        $sql = "";
    }
    /**
     * 添加用户
     * Enter description here ...
     * @param unknown_type $params
     */
    public function addUser($params)
    {
        $this->add($this->_vote_user, $params);
    }
    
    /**
     * 配置用户信息
     * Enter description here ...
     * @param unknown_type $v
     * @param unknown_type $where
     */
    public function updateUserQlnum($bianhao, $zhongjiang = FALSE)
    {
        if ($zhongjiang) {
            $sql = "UPDATE $this->_vote_user SET `ql_num`=`ql_num`+1,`zjqk`=`zjqk`+1 WHERE `bianhao`='$bianhao'";
        } else {
            $sql = "UPDATE $this->_vote_user SET `ql_num`=`ql_num`+1 WHERE `bianhao`='$bianhao'";
        }
        $this->exec($sql);
    }
    
    /**
     * 获取用户
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getVoteUser($where)
    {
        return $this->getOne($this->_vote_user, $where);
    }
    
    /**
     * 获取用户
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getWxUser($where)
    {
        return $this->getOne($this->_wx_user, $where);
    }
    
    public function saveWxUser($v, $where)
    {
        $this->update($this->_wx_user, $v, $where);
    }
    
    /**
     * 获取幸运用户
     * Enter description here ...
     * @param unknown_type $where
     */
    public function getLuckByWhere($where=NULL, $key=NULL, $order=NULL, $limit=10)
    {
        return $this->getAll($this->_vote_luck, $where, $key, $order, $limit);
    }
    
    /**
     * 返回VoteModel
     * @return VoteModel
     */
    public static function instance()
    {
        return parent::_instance(__CLASS__);
    }
}